<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('xmwms.warehouse.serial_no')"
                        type="string"
                        prop="serial_no"
                        :input-attr="{ step: 1 }"
                        v-model.number="baTable.form.items!.serial_no"
                        :placeholder="t('Please input field', { field: t('xmwms.warehouse.serial_no') })"
                    />
                    <FormItem
                        :label="t('xmwms.warehouse.name')"
                        type="string"
                        v-model="baTable.form.items!.name"
                        prop="name"
                        :placeholder="t('Please input field', { field: t('xmwms.warehouse.name') })"
                    />
                    <el-form-item :label="t('xmwms.warehouse.responsible_person')" prop="responsible_person">
                        <el-select
                            v-model="baTable.form.items!.responsible_person"
                            :placeholder="t('Please input field', { field: t('xmwms.warehouse.responsible_person') })"
                        >
                            <el-option v-for="(item, index) in adminList" :key="index" :value="item.id" :label="item.nickname"></el-option>
                        </el-select>
                    </el-form-item>
                    <FormItem
                        :label="t('xmwms.warehouse.mobile')"
                        type="string"
                        v-model="baTable.form.items!.mobile"
                        prop="mobile"
                        :placeholder="t('Please input field', { field: t('xmwms.warehouse.mobile') })"
                    />
                    <FormItem
                        :label="t('xmwms.warehouse.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{ content: { 0: t('xmwms.warehouse.status 0'), 1: t('xmwms.warehouse.status 1') } }"
                        :placeholder="t('Please select field', { field: t('xmwms.warehouse.status') })"
                    />
                    <FormItem
                        :label="t('xmwms.warehouse.remark')"
                        type="textarea"
                        v-model="baTable.form.items!.remark"
                        prop="remark"
                        :placeholder="t('Please input field', { field: t('xmwms.warehouse.remark') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from '/@/utils/axios'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    serial_no: [buildValidatorData({ name: 'required', title: t('xmwms.warehouse.serial_no') })],
    name: [buildValidatorData({ name: 'required', title: t('xmwms.warehouse.name') })],
    responsible_person: [buildValidatorData({ name: 'required', title: t('xmwms.warehouse.admin') })],
})

let adminList = ref(<any>[]) // 管理员列表
//获取管理员列表
const getAdminList = () => {
    createAxios({
        url: '/index.php/admin/user.user/select',
        method: 'GET',
    }).then((res) => {
        adminList.value = res.data.list.map((item: any) => {
            return { id: item.id.toString(), nickname: item.nickname }
        })
    })
}
onMounted(() => {
    getAdminList() //获取管理员列表
})
</script>

<style scoped lang="scss"></style>
